import Vue from 'vue';
import axios from 'axios';
// import { Modal } from 'view-design';
// import service from '../assets/js/serviceConfig';
// import $bizApi from '../assets/js/api';

// 配置超时时间
axios.defaults.timeout = 100000

let loadingIndex=0;
// var asyncLoadingApis=[
//     $bizApi.qryCustomerOrderList,
//     $bizApi.qryProdInstCntList
// ];

axios.defaults.withCredentials = false
//请求拦截
axios.interceptors.request.use(function (config) {
    //获取登录者信息，没有值则跳转登录页
    // let staffName = sessionStorage.getItem('staffName');
    // let staffId = sessionStorage.getItem('staffId');
    // let staffCode = sessionStorage.getItem('staffCode');
    // if(!staffName || !staffId || !staffCode){
    //     alert('登录超时，请重新登录');
    //     window.location.href = service.loginUrl;
    //     return false;
    // }


	//成功
    //debugger
    //console.log(asyncLoadingApis)
    // if(asyncLoadingApis.indexOf(config.url)==-1){
        Vue.prototype.$loading.show();
        loadingIndex++;
    // }
	//Vue.prototype.$serverDialog.show(config);
	return config;
},function (error){
	//失败
	return Promise.reject(error);
});
//响应拦截
axios.interceptors.response.use(function (response) {
    //如果接口返回值为'<!DOCTYPE html>',则提示登录超时
    // let str = response.data;
    // if(typeof(str)=='string' && str.substring(0,9) == '<!DOCTYPE'){
    //     alert('登录超时，请重新登录');
    //     window.location.href = service.loginUrl;
    //     return false;
    // }
    //
    // //成功
    // if(asyncLoadingApis.indexOf(response.config.url)==-1) {
        loadingIndex--;
    // }
    if(loadingIndex<=0){
		Vue.prototype.$loading.hide();
    }
	return response;
}, function (error) {
	//失败
    // if(asyncLoadingApis.indexOf(error.config.url)==-1) {
        loadingIndex--;
    // }
	if(loadingIndex<=0){
		Vue.prototype.$loading.hide();
	}
	Vue.prototype.$serverDialog.show(error);

	return Promise.reject(error);
});

export default axios;
